/**
* detectFace.cpp
*/

#include "FaceDetector.h"
#include <iostream>
#include "../util/tinyxml2.h"

using namespace std;
using namespace tinyxml2;

int main(int argc, char** argv) {

	string xmlFilePath;
	if (argc >= 2) {
		xmlFilePath =  argv[1];
	}

	// Load photos xml
	XMLDocument input;
	input.LoadFile((xmlFilePath + "/photos.xml").c_str());
	
	// Create faces xml file
	XMLDocument output;
	XMLDeclaration* declaration = output.NewDeclaration();
	output.InsertEndChild(declaration);
	int faceId = 1;
	for (XMLElement* photoElement = input.FirstChildElement("photo"); 
		 photoElement != NULL;
		 photoElement = photoElement->NextSiblingElement("photo")) {

		// Get photo id
		XMLElement* photoIdElement = photoElement->FirstChildElement("id");
	 	XMLText* photoIdText = photoIdElement->FirstChild()->ToText();
	 	int photoId = atoi(photoIdText->Value());
		
		XMLElement* photoPathElement = photoElement->FirstChildElement("path");
		XMLText* photoPathText = photoPathElement->FirstChild()->ToText();
		const char* photoPath = photoPathText->Value();
	 	FaceDetector faceDetector(photoPath);

	 	vector<Rect*> faces = faceDetector.getFaces();	

	 	if (faces.size() <= 0) {
	 		cout << "No faces are detected on photo!" << endl;
	 	}
	 	else {
	 		vector<Rect*>::const_iterator it = faces.begin();

	 		for (; it != faces.end(); it++) {
	 			// Create a face node
	 			XMLElement* faceElement = output.NewElement("face");
		
	 			// Create face id element
	 			ostringstream stringStream;
	 			stringStream << faceId++;
	 			XMLElement* faceIdElement = output.NewElement("id");
	 			XMLText* faceIdText = output.NewText(stringStream.str().c_str());
	 			faceIdElement->InsertEndChild(faceIdText);
	 			faceElement->InsertEndChild(faceIdElement);
		
	 			// Create photo id element
	 			stringStream.str(""); stringStream.clear();
	 			stringStream << photoId;
	 			XMLElement* photoIdElement = output.NewElement("photo_id");
	 			XMLText* photoIdText = output.NewText(stringStream.str().c_str());
	 			photoIdElement->InsertEndChild(photoIdText);
	 			faceElement->InsertEndChild(photoIdElement);
		
	 			// Create top left x element
	 			stringStream.str(""); stringStream.clear();
	 			stringStream << (*it)->getTopLeft().getX();
	 			XMLElement* pos = output.NewElement("top_left_x");
	 			XMLText* posText = output.NewText(stringStream.str().c_str());
	 			pos->InsertEndChild(posText);
	 			faceElement->InsertEndChild(pos);
		
	 			// Create top left y element
	 			stringStream.str(""); stringStream.clear();
	 			stringStream << (*it)->getTopLeft().getY();
	 			pos = output.NewElement("top_left_y");
	 			posText = output.NewText(stringStream.str().c_str());
	 			pos->InsertEndChild(posText);
	 			faceElement->InsertEndChild(pos);
		
	 			// Create bottom right x element
	 			stringStream.str(""); stringStream.clear();
	 			stringStream << (*it)->getBottomRight().getX();
	 			pos = output.NewElement("bottom_right_x");
	 			posText = output.NewText(stringStream.str().c_str());
	 			pos->InsertEndChild(posText);
	 			faceElement->InsertEndChild(pos);
		
	 			// Create top left x element
	 			stringStream.str(""); stringStream.clear();
	 			stringStream << (*it)->getBottomRight().getY();
	 			pos = output.NewElement("bottom_right_y");
	 			posText = output.NewText(stringStream.str().c_str());
	 			pos->InsertEndChild(posText);
	 			faceElement->InsertEndChild(pos);
		
	 			// Add face element
	 			output.InsertEndChild(faceElement);
		
	 			cout << "Face: (" << (*it)->getTopLeft().getX() << ", " << (*it)->getTopLeft().getY() << ") (" 
	 				 << (*it)->getBottomRight().getX() << ", " << (*it)->getBottomRight().getY() << ")" << endl;
	 		}	
	 	}
	}
	
	// Save XML file
	output.SaveFile((xmlFilePath + "/faces.xml").c_str());
	
	return 0;
}